<?php

$lib_path = '../lib/php';
set_include_path(get_include_path() . PATH_SEPARATOR . $lib_path);

require_once('cache_packet.php');

///////////////////////////////////////////////////////////////////////

function assert_failure($filename, $lineno, $expression)
{
    echo "Assert Failure at $filename:$lineno\n";
}

// Set our assert options
assert_options(ASSERT_ACTIVE,    true);
assert_options(ASSERT_BAIL,      true);
assert_options(ASSERT_WARNING,   false);
assert_options(ASSERT_CALLBACK,  'assert_failure');

class    TestCachePacket
{

    public function setUp()
    {
    }
    
    public function tearDown()
    {
    }
        
    public function test_request()
    {
        $apkt = new CachePacket();
        $apkt->key = 'KEY--TEST--KEY';
        $apkt->timeout = 123456;
        $apkt->data = 'DATA#TEST#DATA';
        $apkt->status = 200;
        $apkt->setRequest();
        
        assert(true === $apkt->isRequest());
        assert(false === $apkt->isResponse());

        // test INSERT action 
        $apkt->setInsertAction();
        assert(true === $apkt->isInsertAction());
        assert(false === $apkt->isEraseAction());
        assert(false === $apkt->isLookupAction());
        
        $record = $apkt->encodeRequest();
        $bpkt = new CachePacket();
        $bpkt->decodeRequest($record);
        assert(true === $bpkt->isRequest());
        assert(false === $bpkt->isResponse());
        assert(true === $bpkt->isInsertAction());
        assert(false === $bpkt->isEraseAction());
        assert(false === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        assert($apkt->data === $bpkt->data);
        assert($apkt->timeout === $bpkt->timeout);
        // REQUEST ignore status field 
        ///assert($apkt->status === $bpkt->status);
        
        // test ERASE action 
        $apkt->setEraseAction();
        assert(false === $apkt->isInsertAction());
        assert(true === $apkt->isEraseAction());
        assert(false === $apkt->isLookupAction());
        
        $record = $apkt->encodeRequest();
        $bpkt = new CachePacket();
        $bpkt->decodeRequest($record);
        assert(true === $bpkt->isRequest());
        assert(false === $bpkt->isResponse());
        assert(false === $bpkt->isInsertAction());
        assert(true === $bpkt->isEraseAction());
        assert(false === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        // ERASE REQUEST ignore data field 
        ///assert($apkt->data === $bpkt->data);
        // ERASE REQUEST ignore timeout field 
        ///assert($apkt->timeout === $bpkt->timeout);
        // REQUEST ignore status field 
        ///assert($apkt->status === $bpkt->status);
        
        // test LOOKUP action 
        $apkt->setLookupAction();
        assert(false === $apkt->isInsertAction());
        assert(false === $apkt->isEraseAction());
        assert(true === $apkt->isLookupAction());
        
        $record = $apkt->encodeRequest();
        $bpkt = new CachePacket();
        $bpkt->decodeRequest($record);
        assert(true === $bpkt->isRequest());
        assert(false === $bpkt->isResponse());
        assert(false === $bpkt->isInsertAction());
        assert(false === $bpkt->isEraseAction());
        assert(true === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        // LOOKUP REQUEST ignore data field 
        ///assert($apkt->data === $bpkt->data);
        // LOOKUP REQUEST ignore timeout field 
        ///assert($apkt->timeout === $bpkt->timeout);
        // REQUEST ignore status field 
        ///assert($apkt->status === $bpkt->status);
        
    }
    
    public function test_response()
    {
        $apkt = new CachePacket();
        $apkt->key = 'KEY--TEST--KEY';
        $apkt->timeout = 123456;
        $apkt->data = 'DATA#TEST#DATA';
        $apkt->status = 200;
        $apkt->setResponse();
        
        assert(false === $apkt->isRequest());
        assert(true === $apkt->isResponse());

        // test INSERT action 
        $apkt->setInsertAction();
        assert(true === $apkt->isInsertAction());
        assert(false === $apkt->isEraseAction());
        assert(false === $apkt->isLookupAction());
        
        $record = $apkt->encodeResponse();
        $bpkt = new CachePacket();
        $bpkt->decodeResponse($record);
        assert(false === $bpkt->isRequest());
        assert(true === $bpkt->isResponse());
        assert(true === $bpkt->isInsertAction());
        assert(false === $bpkt->isEraseAction());
        assert(false === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        // RESPONSE ignore data field 
        ///assert($apkt->data === $bpkt->data);
        assert($apkt->timeout === $bpkt->timeout);
        assert($apkt->status === $bpkt->status);
        
        // test ERASE action 
        $apkt->setEraseAction();
        assert(false === $apkt->isInsertAction());
        assert(true === $apkt->isEraseAction());
        assert(false === $apkt->isLookupAction());
        
        $record = $apkt->encodeResponse();
        $bpkt = new CachePacket();
        $bpkt->decodeResponse($record);
        assert(false === $bpkt->isRequest());
        assert(true === $bpkt->isResponse());
        assert(false === $bpkt->isInsertAction());
        assert(true === $bpkt->isEraseAction());
        assert(false === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        // ERASE RESPONSE ignore data field 
        ///assert($apkt->data === $bpkt->data);
        // ERASE RESPONSE ignore timeout field 
        ///assert($apkt->timeout === $bpkt->timeout);
        assert($apkt->status === $bpkt->status);
        
        // test LOOKUP action 
        $apkt->setLookupAction();
        assert(false === $apkt->isInsertAction());
        assert(false === $apkt->isEraseAction());
        assert(true === $apkt->isLookupAction());
        
        $record = $apkt->encodeResponse();
        $bpkt = new CachePacket();
        $bpkt->decodeResponse($record);
        assert(false === $bpkt->isRequest());
        assert(true === $bpkt->isResponse());
        assert(false === $bpkt->isInsertAction());
        assert(false === $bpkt->isEraseAction());
        assert(true === $bpkt->isLookupAction());
        assert($apkt->key === $bpkt->key);
        assert($apkt->data === $bpkt->data);
        // LOOKUP RESPONSE ignore timeout field 
        ///assert($apkt->timeout === $bpkt->timeout);
        assert($apkt->status === $bpkt->status);
    }

    public function run()
    {
        $methods = array('test_request', 'test_response');
        foreach ($methods as $method) {
            $this->setUp();
            $this->$method();
            $this->tearDown();
            echo '.';
        }
        echo "\n";
    }    // run()
    
}    // class TestCachePacket

$testcase = new TestCachePacket();
$testcase->run();
